﻿#include "audio_player_utils.h"



// 辅助函数：将UTF-8字符串转换为宽字符串
std::wstring utils::utf8ToWide(const std::string& utf8Str) {
    // 计算所需的宽字符缓冲区大小
    int wideStrLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);

    if (wideStrLen == 0) {
        // 转换失败，返回空字符串或抛出异常
        DWORD error = GetLastError();
        std::cerr << "UTF-8 to wide conversion failed: " << error << std::endl;
        return L"";
    }

    // 创建宽字符字符串
    std::wstring wideStr(wideStrLen, 0);

    // 执行转换
    if (MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideStrLen) == 0) {
        DWORD error = GetLastError();
        std::cerr << "UTF-8 to wide conversion failed: " << error << std::endl;
        return L"";
    }

    // 移除末尾的空字符（std::wstring 会自动管理长度）
    wideStr.resize(wideStrLen - 1);

    return wideStr;
}

// 辅助函数: 获取 MCI 错误信息
std::wstring utils::GetMciErrorText(DWORD errorCode) {
    wchar_t buffer[128] = { 0 };
    if (mciGetErrorString(errorCode, buffer, 128)) {
        return L"MciErrorText: " + std::wstring(buffer);
    }
    return L"MciErrorText: Unknown Error: " + std::to_wstring(errorCode);
}